﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Entity.Car_Rental;
using Data.Car_Rental;
using Web.Commons;
using Entity;
using VCommons;
using System.Reflection;

namespace Car_Rental.Controllers.Helpers
{
    /// <summary>
    /// 前台公用方法
    /// </summary>
    public class CommonMethod : BaseController
    {

        #region Constructors and Declares
        static IUserRepository iUserRepository;
        static CommonMethod()
        {
            iUserRepository = new UserRepository();
        }
        #endregion

        #region 登陆与登出
        /// <summary>
        /// 统一登陆
        /// </summary>
        /// <param name="controller">当前controller上下文</param>
        /// <param name="entity">用户实体</param>
        /// <param name="formUrl">是否从指定url过来的POST请求</param>
        public static void UserLogOnModule(Controller controller, UserBases entity, string formUrl)
        {
            if (controller.Request.FilePath.ToLower() == formUrl.ToLower()) //判断是否从指定地址提交过来
            {
                string returnUrl = controller.Request.QueryString["returnUrl"];
                if (string.IsNullOrEmpty(entity.Name) || string.IsNullOrEmpty(entity.Password))
                {
                    if (string.IsNullOrEmpty(entity.Name))
                        controller.ModelState.AddModelError("Name", "请填写用户名");
                    if (string.IsNullOrEmpty(entity.Password))
                        controller.ModelState.AddModelError("Password", "请填写密码");
                }
                else
                {
                    entity.Password = entity.Password;
                    VMessage vm = iUserRepository.Login(entity);
                    if (vm.IsComplete)
                    {
                        #region 写cookies
                        Web.CurrentLoginInfo.Serialization((vm.Entity as UserBases).UserID, (vm.Entity as UserBases).Name, (vm.Entity as UserBases).Email);
                        #endregion
                        if (!String.IsNullOrEmpty(returnUrl))
                        {
                            controller.Response.Redirect(returnUrl);
                        }
                        else
                        {
                            if (formUrl.ToLower() != "/account/register")
                                controller.HttpContext.Response.Redirect(controller.Url.Action("LogOn", "UC"));
                        }
                    }
                    else
                    {
                        controller.ModelState.AddModelError("", "用户名或密码错误");
                    }
                }
            }
        }
        /// <summary>
        /// 统一登陆
        /// </summary>
        /// <param name="controller">当前controller上下文</param>
        /// <param name="entity">用户实体</param>
        public static void UserLogOnModule(Controller controller, UserBases entity)
        {
            UserLogOnModule(controller, entity, controller.Request.FilePath);
        }
        /// <summary>
        /// 统一登出
        /// </summary>
        /// <returns></returns>
        public static ActionResult UserLogOutModule(Controller controller)
        {
            string returnUrl = controller.Request.QueryString["returnUrl"];

            Web.CurrentLoginInfo.Clear();

            if (!String.IsNullOrEmpty(returnUrl))
            {
                controller.Response.Redirect(returnUrl);
            }
            else
            {
                controller.Response.Redirect(controller.Url.Action("index", "home"));
            }
            return null;
        }

        #endregion
    }
}
